﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace testApp
{
    public partial class fmMain : Form
    {

        public fmMain()
        {
            InitializeComponent();
            UpdateMainForm(0);
        }
       

        public void FindNodeByTag(TreeNodeCollection nodes,int tag)
        {
            foreach (TreeNode tn in nodes)
            {
                if (tn.Tag.ToString() == tag.ToString())
                {
                    treeView1.Focus();
                    treeView1.SelectedNode = tn;
                }
                else
                {
                    FindNodeByTag(tn.Nodes,tag);
                }
            }
        }

        public List<int> FindAllChilds(TreeNode node, List<int> ids)
        {
            ids.Add(int.Parse(node.Tag.ToString()));
            if (node.Nodes.Count > 0)
            {
                foreach (TreeNode tn in node.Nodes)
                {
                    FindAllChilds(tn, ids);
                }
            }
            return ids;
        }

        public void UpdateMainForm(int selectedNode)
        {
               
            treeView1.Nodes.Clear();
            treeView1.BeginUpdate();
            Department newDep = dm.GetDepartmentByID(0);
            TreeNode newNode = new TreeNode(newDep.DepartmentName);
            treeView1.Nodes.Add(newNode);
            newNode.Tag = 0;
            AddChildrenToTree(treeView1.Nodes[0], 0);
            treeView1.EndUpdate();
            treeView1.Refresh();
            FindNodeByTag(treeView1.Nodes,selectedNode);
        }

        public void AddChildrenToTree(TreeNode node, int id)
        {
            var deps = dm.GetDepartmentsByParentID(id);
            if (deps != null)
            {
                foreach(Department dep in deps)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = dep.DepartmentName;
                    newNode.Tag = dep.DepartmentID.ToString();
                    node.Nodes.Add(newNode);
                    AddChildrenToTree(newNode , (int)dep.DepartmentID);
                }
            }
        }

        private void btnAddDepartment_Click(object sender, EventArgs e)
        {
            var d = new Department(); // this.treeView1.SelectedNode.Tag as Department;
            if (DepartmentForm.Execute(d))
                UpdateMainForm(d);

            int selNode = int.Parse(this.treeView1.SelectedNode.Tag.ToString());
            DepartmentForm depForm = new DepartmentForm(selNode, "add");
            depForm.ShowDialog();
            if (depForm.DialogResult == DialogResult.OK)
            {

                UpdateMainForm(depForm.dbResultID);
            }
        }

        private void btnEditDepartment_Click(object sender, EventArgs e)
        {
            int selNode = int.Parse(this.treeView1.SelectedNode.Tag.ToString());
            DepartmentForm depForm = new DepartmentForm(selNode, "edit");
            depForm.ShowDialog();

            if (depForm.DialogResult == DialogResult.OK)
            {
                UpdateMainForm(depForm.dbResultID);
            }
        }

        private void btnDeleteDepartment_Click(object sender, EventArgs e)
        {
            int selNode = int.Parse(this.treeView1.SelectedNode.Tag.ToString());
            if (selNode > 0)
            {
                var res = MessageBox.Show("Do you realy want to delete department " + this.treeView1.SelectedNode.Text.ToString() + " ,all sub-departments and personal?", "Deleting Department", MessageBoxButtons.YesNo);
                if (res == System.Windows.Forms.DialogResult.Yes)
                {
                    dm.DelDepartmentByID(selNode);
                    UpdateMainForm(0);
                }
            }
            else
            {
                var res = MessageBox.Show("You can't delete the root department " + this.treeView1.SelectedNode.Text.ToString(), "Deleting Department", MessageBoxButtons.OK);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.checkBox1.Checked)
            {
                //This is Multinode
                this.dataGridView1.DataSource = null;
                List<vw_EmployeeList> lstEmployee = new List<vw_EmployeeList>();
                List<int> lstIds = new List<int>();
                TreeNode selNode = this.treeView1.SelectedNode;
                lstIds = FindAllChilds(selNode, lstIds);
                lstEmployee = dm.GetEmployeeListByIDList(lstIds).ToList();
                this.dataGridView1.DataSource = lstEmployee;

            }
            else
            {
                // This is single Node
                TreeNode selNode = this.treeView1.SelectedNode;
                this.dataGridView1.DataSource = dm.GetEmployeeListByDepID(int.Parse(selNode.Tag.ToString()));
                dataGridView1.Refresh();

            }
            
        }

        private void btnAddEmployee_Click(object sender, EventArgs e)
        {
            int selEmp = 0;
            int selNode = int.Parse(this.treeView1.SelectedNode.Tag.ToString());
            EmployeeForm empForm = new EmployeeForm(selEmp,selNode, "add");
            empForm.ShowDialog();
            if (empForm.DialogResult == DialogResult.OK)
            {

                UpdateMainForm(selNode);
            }
        }

        private void btnEditEmployee_Click(object sender, EventArgs e)
        {
            int selEmp = int.Parse(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
            int selNode = int.Parse(this.treeView1.SelectedNode.Tag.ToString());
            EmployeeForm empForm = new EmployeeForm(selEmp, selNode, "edit");
            empForm.ShowDialog();
            if (empForm.DialogResult == DialogResult.OK)
            {

                UpdateMainForm(selNode);
            }
        }

        private void btnDeleteEmployee_Click(object sender, EventArgs e)
        {
            int selEmp = int.Parse(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
            int selNode = int.Parse(this.treeView1.SelectedNode.Tag.ToString());
            if (selEmp > 0)
            {
                var res = MessageBox.Show("Do you realy want to delete employee " , "Deleting Employee", MessageBoxButtons.YesNo);
                if (res == System.Windows.Forms.DialogResult.Yes)
                {
                    dm.DelDepartmentByID(selEmp);
                    UpdateMainForm(selNode);
                }
            }
          
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            int selNode = int.Parse(this.treeView1.SelectedNode.Tag.ToString());
            UpdateMainForm(selNode);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int selEmp = int.Parse(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
            int selNode = int.Parse(this.treeView1.SelectedNode.Tag.ToString());
            EmployeeForm empForm = new EmployeeForm(selEmp, selNode, "show");
            empForm.ShowDialog();
        }
    }
}
